package com.xf.sys.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xf.pm.common.PageData;
import com.xf.pm.common.PageForm;
import com.xf.pm.util.PageUtils;
import com.xf.sys.entity.SysMenu;
import com.xf.sys.entity.TreeNode;
import com.xf.sys.mapper.SysMenuMapper;
import com.xf.sys.service.ISysMenuService;

/**
 *SysMenuService.java
 *SysMenuService
 *author:lb
 *date:2016年11月2日
 */
@Service
public class SysMenuService implements ISysMenuService {

	private final Logger logger =  LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	private SysMenuMapper sysMenuMapper;
	
	@Override
	public int insertSysMenu(SysMenu sysMenu) {
		int result=0;
		SysMenu sMenu=sysMenuMapper.selectSysMenuByName(sysMenu.getMenuName());
		if(null==sMenu){
			if(null==sysMenu.getParentId()){
				sysMenu.setParentId(0);
				Integer maxId=sysMenuMapper.selectMaxMenuId(sysMenu.getParentId());
				sysMenu.setMenuId(maxId+1);
			}else{
				Integer maxId=sysMenuMapper.selectMaxMenuId(sysMenu.getParentId());
				if(null==maxId){//父ID往后拼接两位
					sysMenu.setMenuId(Integer.valueOf(sysMenu.getParentId()+"01"));
				}else{
					sysMenu.setMenuId(maxId+1);
				}
			}
			result=sysMenuMapper.insertSysMenu(sysMenu);
		}
		return result;
	}

	@Override
	public int deleteSysMenu(Integer[] menuId) {
		int result=0;
		List<SysMenu> list=sysMenuMapper.selectSysMenuParentId(menuId);
		if(list.isEmpty()){
			result=sysMenuMapper.deleteSysMenu(menuId);
		}
		return result;
	}

	@Override
	public int updateSysMenu(SysMenu sysMenu) {
		return sysMenuMapper.updateSysMenu(sysMenu);
	}

	@Override
	public SysMenu selectSysMenu(Integer roleId) {
		return sysMenuMapper.selectSysMenuById(roleId);
	}

	@Override
	public PageData selectSysMenuList(PageForm form,SysMenu sysMenu) {
		PageUtils.preparePaging(form);
		List<SysMenu> list = null;
		PageData listDate = null;
		try {
			list = sysMenuMapper.selectSysMenuList(sysMenu);
			listDate = PageUtils.wrapPageData(list);
		} catch (Exception e) {
			logger.info("查询菜單数据失败!");
			e.printStackTrace();
		}
		return listDate;
	}

	@Override
	public List<TreeNode> listTree(Integer roleId) {
		List<TreeNode> trees = new ArrayList<TreeNode>();
		List<SysMenu> list =null;
		if(roleId==1){//如果是管理员
			list =sysMenuMapper.selectSysMenuList(null);
		}else{
			list =sysMenuMapper.selectSysMenuRole(roleId);
		}
		
		for (SysMenu sysMenu : list)
		{
			if (sysMenu.getParentId()==0)
			{
				TreeNode tree = new TreeNode();
				List<TreeNode> listTree = createTree(list, sysMenu.getMenuId());
				if (listTree != null &&!listTree.isEmpty())
				{
					tree.setId(String.valueOf(sysMenu.getMenuId()));
					tree.setName(sysMenu.getMenuName());
					tree.setIcon(sysMenu.getMenuImg());
					tree.setChildren(listTree);
					trees.add(tree);
				}else{
					tree.setId(String.valueOf(sysMenu.getMenuId()));
					tree.setName(sysMenu.getMenuName());
					tree.setIcon(sysMenu.getMenuImg());
//					tree.setChildren(listTree);
					trees.add(tree);
				}
			}
		}
		return trees;
	}
	
	private List<TreeNode> createTree(List<SysMenu> list, Integer parentId)
	{
		List<TreeNode> trees = new ArrayList<TreeNode>();
		for (SysMenu sysMenu : list)
		{
			if (parentId==sysMenu.getParentId())
			{
				TreeNode tree = new TreeNode();
				tree.setId(String.valueOf(sysMenu.getMenuId()));
				tree.setName(sysMenu.getMenuName());
				tree.setIcon(sysMenu.getMenuImg());
				trees.add(tree);
			}
		}
		return trees;
	}

}
